package com.ygkj.demo

import android.content.res.Configuration
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_web_view.*


class WebViewActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_web_view)

//        startActivity(Intent(this, Viewpage2Activity::class.java))

//        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        /**
         * 0  + 90
         * 90 + 90
         */
        var flag = 0
        btn1.setOnClickListener {

            if (flag == 0 || flag == 180) {
                view.rotation = 0f
                view.requestLayout()
            } else {

                setAngle(0)
            }
            flag = 0
        }
        btn2.setOnClickListener {
           if(flag==90||flag==270){
               view.rotation = 90f
               view.requestLayout()
           }else{
            setAngle(90)
           }
            flag = 90

        }
        btn3.setOnClickListener {
            if (flag == 0 || flag == 180) {
                view.rotation = 180f
                view.requestLayout()
            } else {

                setAngle(180)
            }
            flag = 180
        }
        btn4.setOnClickListener {
            if(flag==90||flag==270){
                view.rotation = 270f
                view.requestLayout()
            }else {
                setAngle(270)
                flag = 270
            }

        }

    }

    private fun setAngle(angle: Int) {

        val w: Int = view.width
        val h: Int = view.height
        view.rotation = angle.toFloat()
        if (angle % 180 == 90) {
            view.translationX = ((w - h) / 2).toFloat()
            view.translationY = ((h - w) / 2).toFloat()

        } else {
            view.translationX = 0f
            view.translationY = 0f
        }
        val lp = view.layoutParams
        lp.height = w
        lp.width = h

        view.requestLayout()
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
    }
}